package com.android.fragmentdemo.thread15

import java.util.TreeMap
import java.util.concurrent.locks.ReentrantReadWriteLock

class Demo15 {

    private val mDataMap = TreeMap<String, Data>()

    private val mWriteReadLock = ReentrantReadWriteLock()

    private val mReadLock = mWriteReadLock.readLock()

    private val mWriteLock = mWriteReadLock.writeLock()

    fun get(key: String): Data? {
        mReadLock.lock()
        try {
            return mDataMap[key]
        } finally {
            mReadLock.unlock()
        }
    }

    fun put(key: String, value: Data) {
        mWriteLock.lock()
        try {
           mDataMap[key] = value
        } finally {
            mWriteLock.unlock()
        }
    }

    fun getAllKeys(): Set<String> {
        mReadLock.lock()
        try {
            return mDataMap.keys
        } finally {
            mReadLock.unlock()
        }
    }

    data class Data(val name: String, val desc: String)
}